package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OerderSetingService;
import com.itheima.untils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    OerderSetingService oerderSetingService;
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting>orderSettingList=new ArrayList<>();
            for (String[] string : strings) {
                OrderSetting orderSetting=new OrderSetting();
                orderSetting.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(string[0]));
                orderSetting.setNumber(Integer.valueOf(string[1]));
                orderSettingList.add(orderSetting);
            }
                oerderSetingService.edit(orderSettingList);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting>orderSettingList=oerderSetingService.findByMonth(month);
            List<Map<String,Object>>mapList=new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object>map=new HashMap<>();
                map.put("date",new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                mapList.add(map);
            }
            System.out.println(mapList);
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }

    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting>orderSettingList=new ArrayList<>();
            System.out.println(orderSetting);
            orderSettingList.add(orderSetting);
            oerderSetingService.edit(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }

    }

}
